<?php

# lemur cms by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Nov. 13, 2008

class admin extends functions {
	public function __construct () {
		$this->db_connect();
		$this->register_site_info();
	}
	public function set_validation($user, $pass) {
		session_start();
		$_SESSION['username'] = sanitize_sql_string($user);
		$_SESSION['password'] = sha1(sanitize_sql_string($pass));
		if($this->validate() == false) {
			$_SESSION = array();
			session_destroy();
			return false;
		}
		else {
			return true;
		}
	}
	public function isAdmin () {
		$numArg = func_get_args();
		foreach($numArg as $key => $value) {
			$levels .= " level = '$value' or";
		}
		$sql = "SELECT * FROM ".DBTABLEPREFIX."users WHERE $levels level = 'aoiw'";
		$query = $this->db_query($sql) or die(mysql_error());
		$is_admin = false;
		while($row = mysql_fetch_array($query)) {
			if($this->page_info['user']['value'] == $row['username']) {
				$is_admin = true;
			}
		}
		return $is_admin;
	}
	public function adminNavigation () {
		$r = "\n<ul>\n";
		$contents = $this->openFile(BASE_URL."includes/admin/pages/main-page-list.inc");
		$menu = explode("\n", $contents);
		foreach ($menu as $key => $value) {
			$safe_value = make_clean_url($value);
			$r .= "\t<li><a href='admin.php?page=$safe_value'>".stripslashes(stripslashes(stripslashes($value)))."</a></li>\n";
		}
		$contents = $this->openFile(BASE_URL."includes/admin/pages/user-page-list.inc");
		$menu = explode("\n", $contents);
		foreach ($menu as $key => $value) {
			$safe_value = make_clean_url($value);
			$r .= "\t<li><a href='admin.php?page=$safe_value'>".stripslashes(stripslashes(stripslashes($value)))."</a></li>\n";
		}
		$r .= "</ul>";
		return $r;
	}
	public function adminPublicPageList ($ul_id ="ul-navigation",$sub_ul_class ="ul-sub-navigation",$lis = "selected",$link = true,$page = "edit-and-delete-pages",$child = false,$inputArray = array()) {
		if($child == true) {
			$r .= "<ul class='$sub_ul_class'>";
		}
		else {
			$r .= "<ul id='$ul_id'>";
		}
		$x = 0;
		foreach($inputArray as $k=>$v) {
			if(is_array($v['children'])) {
				$r .= "\n\t<li id='{$v['id']}' name='{$v['id']}' class='parent";
				$r .= "'>";
				if($link == true) {
					$r .= "<span class='rearrange-page'><a title='Move $k' class='helplink' href='javascript:;' ><img src='".THEME_URL."includes/admin/images/icons/move.png' /></a></span>
					<span class='delete-page' ><a title='Delete $k'class='helplink' href='javascript:;' ><img src='".THEME_URL."includes/admin/images/icons/delete-16x16.png' alt='Delete' /></a></span>
					<a class='admin-edit-link helplink' id='{$v['id']}' href='?page=$page&id=".$v['id']."' title='Edit $k' ><img src='".THEME_URL."includes/admin/images/icons/edit-16x16.png' alt='Edit' />".stripslashes(stripslashes(stripslashes($k)));
					$r .= "</a>";
				}
				$r .= $this->adminPublicPageList($ul_id, $sub_ul_class, $lis, $link, $page, true,$v['children']);
				$r .= "<br class='clear' /></li>";
			}
			else {
				$r .= "\n\t<li name='{$v['id']}' id='{$v['id']}'";
				$r .= ">";
				if($link == true) {
					$r .= "<span class='rearrange-page'><a title='Move $k' class='helplink' href='javascript:;' ><img src='".THEME_URL."includes/admin/images/icons/move.png' /></a></span>
					<span class='delete-page' ><a title='Delete $k'class='helplink' href='javascript:;' ><img src='".THEME_URL."includes/admin/images/icons/delete-16x16.png' alt='Delete' /></a></span>
					<a class='admin-edit-link helplink' id='{$v['id']}' href='?page=$page&id=".$v['id']."' title='Edit $k' ><img src='".THEME_URL."includes/admin/images/icons/edit-16x16.png' alt='Edit' />".stripslashes(stripslashes(stripslashes($k)));
					$r .= "</a>";
				}
				$r .= '<br class="clear" /></li>';
				$x++;
			}
		}
		$r .= "</ul>";
		return $r;
	}
	public function pageContent ($content, $append=0) {
		//$content = htmlspecialchars_decode(htmlentities($content, ENT_QUOTES, false)); 
		if($append == 1) {
			$this->page_info['content']['page_content'] .= $content;
		}
		if($append == -1) {
			$this->page_info['content']['page_content'] = $content.$this->page_info['content']['page_content'];
		}
		if($append == 0) {
			$this->page_info['content']['page_content'] = $content;
		}
	}
	public function pageTitle ($content, $append=0) {
		//$content = htmlspecialchars_decode(htmlentities($content, ENT_QUOTES, false)); 
		if($append == 1) {
			$this->page_info['content']['page_title'] .= $content;
		}
		if($append == -1) {
			$this->page_info['content']['page_title'] = $content.$this->page_info['content']['page_title'];
		}
		if($append == 0) {
			$this->page_info['content']['page_title'] = $content;
		}
	}
	public function displayPage ($page_id = 0, $set = 0) {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."admin_content WHERE id ='$page_id'";
		$content = $this->db_query($sql) or die(mysql_error());
		while($row = mysql_fetch_array($content)) {
			$this->pageTitle(stripslashes($row['title']));
			$this->pageContent(stripslashes($row['title']));
			$this->page_info['content']['sidebar'] = stripslashes($row['sidebar']);
		}
		if($set == 0) {
			return $this->page_info['content']['page_content'];
		}
	}
	
}
?>